package com.financing.platform.service.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.financing.platform.dao.IManagerOrderDao;
import com.financing.platform.dto.ManagerOrderDTO;
import com.financing.platform.dto.ManagerOrderHistoryDTO;
import com.financing.platform.dto.OrderDTO;
import com.financing.platform.dto.OrderTmpDTO;
import com.financing.platform.dto.UserInfoDTO;
import com.financing.platform.service.IManagerOrderService;
import com.financing.platform.service.IOrderService;
import com.financing.platform.util.Dict;
import com.financing.platform.vo.OrderVO;
import com.financing.platform.service.IOrderTmpService;
import com.financing.platform.vo.ManagerOrderVO;

@Service("managerOrderServiceImpl")
@Transactional
public class ManagerOrderServiceImpl extends BaseServiceImpl implements IManagerOrderService {

	@Autowired
	private IManagerOrderDao managerOrderDaoImpl;
	@Autowired
	private IOrderService orderServiceImpl;
	@Autowired
	private IOrderTmpService orderTmpServiceImpl;

	@Override
	public int addManagerOrderUser(ManagerOrderDTO managerOrder, String orderIds, String ptype) throws SQLException {
		orderIds = orderIds.substring(0, orderIds.length() - 1);
		
		if (ptype.equals(Dict.simuProjectType)) {
			// 私募
			List<OrderTmpDTO> orderTmpList = orderTmpServiceImpl.queryInfoByOrderTmpIds(orderIds);
			
			for(OrderTmpDTO ot : orderTmpList){
				managerOrder.setOrderId(ot.getId());
				managerOrder.setOrderType(Dict.simuOrderType);
				managerOrder.setProjectId(ot.getProductId());
				managerOrder.setProjectType(Dict.simuProjectType);
				managerOrder.setFaUserId(ot.getFaId());
				managerOrderDaoImpl.addManagerOrderUser(managerOrder);// ADD
				// 订单分配历史
				doAddHistory(managerOrder);
			}
		} else {
			// 先根据订单ID查询出订单和产品信息
			List<OrderDTO> orderList = orderServiceImpl.queryInfoByOrderIds(orderIds);

			for (OrderDTO o : orderList) {
				// 分配订单管理员
				managerOrder.setOrderId(o.getId());
				
				if(o.getoRemark() != null && (Dict.pevcProjectType).equals(o.getoRemark())){
					managerOrder.setOrderType(Dict.pevcOrderType);
				}else{
					managerOrder.setOrderType(
							(o.getoRemark() != null && ("02").equals(o.getoRemark()) ? Dict.xintuoOrderType : Dict.ziguanOrderType)
							);
				}
				
				managerOrder.setProjectId(o.getoProjectId());
				managerOrder.setProjectType(o.getoRemark());
				managerOrder.setFaUserId(o.getoFAId());
				managerOrderDaoImpl.addManagerOrderUser(managerOrder);// ADD
				// 订单分配历史
				doAddHistory(managerOrder);
			}
		}
		return 1;
	}
	
	public void doAddHistory(ManagerOrderDTO managerOrder) throws SQLException{
		ManagerOrderHistoryDTO managerOrderHistory = new ManagerOrderHistoryDTO();
		managerOrderHistory.setOrderId(managerOrder.getOrderId());
		managerOrderHistory.setOrderType(managerOrder.getOrderType());
		managerOrderHistory.setProjectId(managerOrder.getProjectId());
		managerOrderHistory.setProjectType(managerOrder.getProjectType());
		managerOrderHistory.setFaUserId(managerOrder.getFaUserId());
		managerOrderHistory.setSystemUserIdFrom("");
		managerOrderHistory.setSystemUserIdTo(managerOrder.getSystemUserId());
		managerOrderHistory.setCreateBy(managerOrder.getCreateBy());
		managerOrderHistory.setCreateDate(managerOrder.getCreateDate());
		managerOrderHistory.setUpdateBy(managerOrder.getUpdateBy());
		managerOrderHistory.setUpdateDate(managerOrder.getUpdateDate());
		managerOrderDaoImpl.addManagerOrderUserHistory(managerOrderHistory);// ADD
	}

	@Override
	public int addManagerOrderUserHistory(ManagerOrderHistoryDTO managerOrderHistory) throws SQLException {
		return managerOrderDaoImpl.addManagerOrderUserHistory(managerOrderHistory);
	}

	@Override
	public int updateManagerOrderUser(ManagerOrderDTO managerOrder, String orderIds, String ptype) throws SQLException {
		orderIds = orderIds.substring(0, orderIds.length() - 1);
		List<ManagerOrderVO> managerOrderList = null;
		
		if (ptype.equals(Dict.simuProjectType)) {
			// 私募
			managerOrderList = managerOrderDaoImpl.queryManagerOrderByOrderTmpIds(orderIds);
		} else {
			managerOrderList = managerOrderDaoImpl.queryManagerOrderByOrderIds(orderIds);
		}

		ManagerOrderHistoryDTO managerOrderHistory = null;
		
		for(ManagerOrderVO mo : managerOrderList){
			// 订单回收
			managerOrder.setId(mo.getId());
			managerOrder.setDeleteTag("2");
			managerOrderDaoImpl.updateManagerOrderUser(managerOrder); // UPDATE;
			// 订单分配历史
			managerOrderHistory = new ManagerOrderHistoryDTO();
			managerOrderHistory.setOrderId(mo.getOrderId());
			managerOrderHistory.setOrderType(mo.getOrderType());
			managerOrderHistory.setProjectId(mo.getProjectId());
			managerOrderHistory.setProjectType(mo.getProjectType());
			managerOrderHistory.setFaUserId(mo.getFaUserId());
			managerOrderHistory.setSystemUserIdFrom(mo.getSystemUserId());
			// managerOrderHistory.setSystemUserIdTo(managerOrder.getSystemUserId());
			managerOrderHistory.setSystemUserIdTo(""); // 回收时，订单管理员设置为空
			managerOrderHistory.setCreateBy(managerOrder.getUpdateBy());
			managerOrderHistory.setCreateDate(managerOrder.getUpdateDate());
			managerOrderHistory.setUpdateBy(managerOrder.getUpdateBy());
			managerOrderHistory.setUpdateDate(managerOrder.getUpdateDate());
			managerOrderDaoImpl.addManagerOrderUserHistory(managerOrderHistory);// ADD
		}
		
		// return managerOrderDaoImpl.updateManagerOrderUser(managerOrder);
		return 1;
	}

	@Override
	public List<ManagerOrderVO> queryManagerOrderByOrderIds(String orderIds) throws SQLException {
		return managerOrderDaoImpl.queryManagerOrderByOrderIds(orderIds);
	}
	
	@Override
	public int editManagerOrderByUser(String systemUserId, UserInfoDTO user) throws SQLException {
		List<ManagerOrderVO> list = managerOrderDaoImpl.queryManagerOrderByUser(systemUserId);
		
		ManagerOrderDTO managerOrder = null;
		ManagerOrderHistoryDTO managerOrderHistory = null;
		
		for (ManagerOrderVO mo : list) {
			// 订单回收
			managerOrder = new ManagerOrderDTO();
			managerOrder.setId(mo.getId());
			// managerOrder.setSystemUserId(user.getUserId());
			managerOrder.setSystemUserId("");
			managerOrder.setUpdateBy(user.getUsername());
			managerOrder.setDeleteTag("2");
			managerOrderDaoImpl.updateManagerOrderUser(managerOrder); // UPDATE;

			// 订单分配历史
			managerOrderHistory = new ManagerOrderHistoryDTO();
			managerOrderHistory.setOrderId(mo.getOrderId());
			managerOrderHistory.setOrderType(mo.getOrderType());
			managerOrderHistory.setProjectId(mo.getProjectId());
			managerOrderHistory.setProjectType(mo.getProjectType());
			managerOrderHistory.setFaUserId(mo.getFaUserId());
			managerOrderHistory.setSystemUserIdFrom(mo.getSystemUserId());
			managerOrderHistory.setSystemUserIdTo(user.getUserId());
			managerOrderHistory.setCreateBy(user.getUsername());
			// managerOrderHistory.setCreateDate(managerOrder.getUpdateDate());
			managerOrderHistory.setUpdateBy(user.getUsername());
			// managerOrderHistory.setUpdateDate(managerOrder.getUpdateDate());
			managerOrderDaoImpl.addManagerOrderUserHistory(managerOrderHistory);// ADD
		}
		
		return 1;
	}
	
	@Override
	public List<HashMap<String, Object>> queryFaUserBySystemUser(Map<String,String> faids) throws SQLException{
		return managerOrderDaoImpl.queryFaUserBySystemUser(faids);
	}

	@Override
	public int countOrdersBySUid(ManagerOrderDTO managerOrder) throws SQLException {
		return managerOrderDaoImpl.countOrdersBySUid(managerOrder);
	}

	@Override
	public List<OrderVO> queryOrdersBySUid(ManagerOrderDTO managerOrder) throws SQLException {
		return managerOrderDaoImpl.queryOrdersBySUid(managerOrder);
	}

	@Override
	public void order2Leader(Map<String, Object> toLeaderMap)
			throws SQLException {
		managerOrderDaoImpl.order2Leader(toLeaderMap);
	}
}
